var _n4;
var cc__extends = __extends;
var cc__decorate = __decorate;
Object.defineProperty(exports, "__esModule", {
  value: true
});
var cc__decorator = cc._decorator;
var ccp_ccclass = cc__decorator.ccclass;
var ccp_property = cc__decorator.property;
var def_Coin_fly_to_wallet = function (t) {
  function _ctor() {
    // 如果 t 存在并且可以调用，则执行 t，否则使用当前上下文
    const instance = null !== t && t.apply(this, arguments) || this;

    // 初始化属性
    instance.startPoint = null;
    instance.endPoint = null;
    instance.coinPrefab = null;
    instance.coinPool = null;
    instance.ani_call = null;

    return instance;

  }
  cc__extends(_ctor, t);
  _ctor.prototype.onLoad = function () {
    this.coinPool = new cc.NodePool();
    this.initCoinPool();
  };
  _ctor.prototype.initCoinPool = function (t) {
    // 如果 t 未定义，则设置为 20
    t = t !== undefined ? t : 20;

    // 实例化 coinPrefab 并放入 coinPool
    for (let i = 0; i < t; i++) {
      const coinInstance = cc.instantiate(this.coinPrefab);
      this.coinPool.put(coinInstance);
    }

  };
  _ctor.prototype.playAnim = function (t) {
    // 生成一个随机值，范围在 10 到 25 之间
    let coinFlyDistance = (15 * Math.random()) + 10;

    // 如果 t 存在，则使用 t 的值
    if (t) {
      coinFlyDistance = t;
    }

    // 获取起始点和结束点的位置
    const startPosition = this.startPoint.getPosition();
    const endPosition = this.endPoint.getPosition();

    // 播放硬币飞行动画
    this.playCoinFlyAnim(coinFlyDistance, startPosition, endPosition);

  };
  _ctor.prototype.playCoinFlyAnim = function (t, e, o, n) {
    let context = this; // 使用更具描述性的变量名
    n = (n === undefined) ? 30 : n; // 更简洁的赋值检查

    let currentPoolSize = this.coinPool.size();
    let coinsToInitialize = currentPoolSize > t ? 0 : t - currentPoolSize;

    // 初始化硬币池
    this.initCoinPool(coinsToInitialize);

    // 获取圆形点并进行处理
    let circlePoints = this.getCirclePoints(n, e, t).map(function (point) {
      let coinNode = context.coinPool.get();
      coinNode.setPosition(e);
      context.node.addChild(coinNode);
      return {
        node: coinNode,
        startPosition: e,
        middlePosition: point,
        endPosition: o,
        distance: point.sub(o).mag()
      };
    });

    // 对硬币位置进行排序
    circlePoints.sort((a, b) => a.distance - b.distance)
      .forEach(function (coin, index) {
        let randomDuration1 = 0.1 + 0.1 * Math.random();
        let randomDuration2 = 0.4 + 0.1 * Math.random();
        let endPosition = cc.v3(coin.endPosition); // 提前计算目标位置

        cc.tween(coin.node)
          .to(randomDuration1, { position: cc.v3(coin.middlePosition) })
          .delay(0.01 * index)
          .to(randomDuration2, { position: endPosition }, cc.easeInOut(3))
          .call(function () {
            context.coinPool.put(coin.node);
            if (context.ani_call && circlePoints.length === index + 1) {
              context.ani_call();
            }
          })
          .start();
      });

  };
  _ctor.prototype.getCirclePoints = function (t, e, o, n) {
    // 如果 n 未定义，则设置为 60
    n = (n === undefined) ? 60 : n;

    let pointsArray = [];
    let angleIncrement = Math.PI / 180 * Math.round(360 / o); // 计算角度增量

    for (let index = 0; index < o; index++) {
      let offsetX = e.x + t * Math.sin(angleIncrement * index);
      let offsetY = e.y + t * Math.cos(angleIncrement * index);

      // 生成随机点并加入数组
      pointsArray.push(cc.v3(offsetX + Math.random() * n, offsetY + Math.random() * n, 0));
    }

    return pointsArray;

  };
  cc__decorate([ccp_property(cc.Node)], _ctor.prototype, "startPoint", undefined);
  cc__decorate([ccp_property(cc.Node)], _ctor.prototype, "endPoint", undefined);
  cc__decorate([ccp_property(cc.Prefab)], _ctor.prototype, "coinPrefab", undefined);
  return cc__decorate([ccp_ccclass], _ctor);
}(cc.Component);
exports.default = def_Coin_fly_to_wallet;